//
//  MBSpaceAddMusicViewController.m
//  MiBaoTu
//
//  Created by admin on 16/9/22.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBSpaceAddMusicViewController.h"
#import "MBSpaceAddMusicDetailViewController.h"

@interface MBSpaceAddMusicViewController ()<UITableViewDataSource,UITableViewDelegate> {
    UITableView             *_tableView;
    NSMutableArray          *_mutArr;
}

@end

@implementation MBSpaceAddMusicViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (ISIPAD) {
        [self onUpdateUIForView:self.orientation];
    }
    [self loadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _mutArr = [[NSMutableArray alloc]init];
    // Do any additional setup after loading the view.

    [self setNavBar];
    [self onCreateUI];
    
}

#pragma mark - Actions
#pragma mark - UI
- (void)setNavBar
{
    if (self.type == MBSpaceAddMusicType_Collection) {
        [self setTitleView:@"添加歌曲"];
    }
    else if (self.type == MBSpaceAddMusicType_SetAlbum) {
        NSString *title = [NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"CateName"]];
        [self setTitleView:[NSString stringWithFormat:@"添加歌曲到%@",title]];
    }
    
    UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
    bt.frame = CGRectMake(0, 0, 60, 30);
    [bt setTitle:@"取消" forState:UIControlStateNormal];
    [bt setTitle:@"取消" forState:UIControlStateHighlighted];
    [bt setTitleEdgeInsets:UIEdgeInsetsMake(0, 20, 0, 0)];
    bt.titleLabel.font = [UIFont systemFontOfSize:13.];
    [bt addTarget:self action:@selector(onSelectItemForBack) forControlEvents:UIControlEventTouchUpInside];
    [self setRightBarItems:bt];
    
    UIButton *lbt = [UIButton buttonWithType:UIButtonTypeCustom];
    lbt.frame = CGRectMake(0, 0, 60, 30);
    [lbt setTitle:@"" forState:UIControlStateNormal];
    [lbt setTitle:@"" forState:UIControlStateHighlighted];
    [self setLeftBarItem:lbt];
}

- (void)onCreateUI
{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.view.height) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.backgroundView = nil;
    [self.view addSubview:_tableView];
}

- (void)onUpdateUIForView:(UIDeviceOrientation)orientation
{
    _tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.view.height);
    [_tableView reloadData];
}

- (void)onSelectItemForBack
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 加载数据
- (void)loadData
{
    //下载的歌曲分类
    [_mutArr removeAllObjects];
    NSArray *downedMusicArr = [[FMManageObject standerDefault]onSelectCustomMusicFileCount];
    NSMutableArray *allArr = [[NSMutableArray alloc]init];
    NSMutableArray *ergeArr = [[NSMutableArray alloc]init];
    NSMutableArray *guoxueArr = [[NSMutableArray alloc]init];
    NSMutableArray *gushiArr = [[NSMutableArray alloc]init];
    NSMutableArray *yingwenArr = [[NSMutableArray alloc]init];
    for (NSDictionary *dic in downedMusicArr) {
        NSMutableDictionary *nDic = [[NSMutableDictionary alloc]initWithDictionary:dic];
        [nDic setObject:[dic objectForKey:@"type"] forKey:@"ValueID"];
        
        NSString *type = [nDic objectForKey:@"type"];
        if ([type isEqualToString:@"1"]) {
            [ergeArr addObject:nDic];
        }
        else  if ([type isEqualToString:@"2"]) {
            [guoxueArr addObject:nDic];
        }
        else  if ([type isEqualToString:@"3"]) {
            [gushiArr addObject:nDic];
        }
        else  if ([type isEqualToString:@"4"]) {
            [yingwenArr addObject:nDic];
        }
        [allArr addObject:nDic];
    }
    
    //下载的亲子秀
    NSArray *downedQzixArr = [[FMManageObject standerDefault]onSelectLocalParentChildShowCount];
    NSMutableArray *downedQZXMutArr = [[NSMutableArray alloc]init];
    for (NSDictionary *dic in downedQzixArr) {
        NSString *type = [dic objectForKey:@"type"];
        if (![type hasPrefix:@"7-"]) {
            [downedQZXMutArr addObject:dic];
        }
    }
    int i = 1;
    for (NSDictionary *dic in downedQZXMutArr) {
        [allArr addObject:dic];
        ++i;
    }
    
    [_mutArr addObject:allArr];
    [_mutArr addObject:ergeArr];
    [_mutArr addObject:guoxueArr];
    [_mutArr addObject:gushiArr];
    [_mutArr addObject:yingwenArr];
    [_mutArr addObject:downedQZXMutArr];
    [_tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"add_music_cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.accessoryType = UITableViewCellAccessoryNone;

        UIImageView *headImgView = [[UIImageView alloc]initWithFrame:CGRectMake(25*PERCEN, 32.5*PERCEN, 35*PERCEN, 35*PERCEN)];
        headImgView.backgroundColor = [UIColor clearColor];
        headImgView.tag = 100;
        [cell.contentView addSubview:headImgView];
        
        UILabel *titleLb = [[UILabel alloc]initWithFrame:CGRectMake(headImgView.right+20*PERCEN, (100*PERCEN-30)/2., 120, 30)];
        titleLb.backgroundColor = [UIColor clearColor];
        titleLb.textAlignment = NSTextAlignmentLeft;
        titleLb.textColor = [UIColor blackColor];
        titleLb.font = [UIFont systemFontOfSize:14.f];
        titleLb.tag = 101;
        [cell.contentView addSubview:titleLb];
        
        UIImage *rightImg = [UIImage imageNamed:@"arrow"];
        UIImageView *rightView= [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-25*PERCEN-rightImg.size.width, (100*PERCEN-rightImg.size.height)/2., rightImg.size.width, rightImg.size.height)];
        rightView.backgroundColor = [UIColor clearColor];
        rightView.image = rightImg;
        rightView.tag = 104;
        [cell.contentView addSubview:rightView];
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 100*PERCEN-1, SCREEN_WIDTH, 1)];
        lineView.backgroundColor = [UIColorFromHex(0xcccccc)colorWithAlphaComponent:.35];
        lineView.tag = 102;
        [cell.contentView addSubview:lineView];
        
        //我的等级
        UILabel *levelLb = [[UILabel alloc]initWithFrame:CGRectMake(rightView.right-30*PERCEN-120, titleLb.top, 120, 30)];
        levelLb.backgroundColor = [UIColor clearColor];
        levelLb.textAlignment = NSTextAlignmentRight;
        levelLb.textColor = UIColorFromHex(0x999999);
        levelLb.font = [UIFont systemFontOfSize:14.f];
        levelLb.tag = 103;
        [cell.contentView addSubview:levelLb];

    }
    UIImageView *headImgView =  (UIImageView *)[cell viewWithTag:100];
    UILabel *titleLb =  (UILabel *)[cell viewWithTag:101];
    UIView *lineView =  (UIView *)[cell viewWithTag:102];
    UILabel *levelLb =  (UILabel *)[cell viewWithTag:103];
    UIImageView *rightView =  (UIImageView *)[cell viewWithTag:104];
    switch (indexPath.section) {
        case 0:
        {
            headImgView.image = [UIImage imageNamed:@"yixiazai_kj"];
            headImgView.frame = CGRectMake(25*PERCEN, (100*PERCEN - 35*PERCEN*headImgView.image.size.height/headImgView.image.size.width)/2., 35*PERCEN, 35*PERCEN*headImgView.image.size.height/headImgView.image.size.width);
            titleLb.text = @"全部下载";
            lineView.hidden = YES;
            levelLb.text = [NSString stringWithFormat:@"%d首",(int)([[_mutArr objectAtIndex:0] count])];
        }
            break;
        case 1:
        {
            switch (indexPath.row) {
                case 0:
                {
                    headImgView.image = [UIImage imageNamed:@"erge_kj"];
                    headImgView.frame = CGRectMake(25*PERCEN, (100*PERCEN - 35*PERCEN*headImgView.image.size.height/headImgView.image.size.width)/2., 35*PERCEN, 35*PERCEN*headImgView.image.size.height/headImgView.image.size.width);
                    titleLb.text = @"儿歌";
                    levelLb.text = [NSString stringWithFormat:@"%d首",(int)([[_mutArr objectAtIndex:1] count])];
                }
                    break;
                case 1:
                {
                    headImgView.image = [UIImage imageNamed:@"guoxue_kj"];
                    headImgView.frame = CGRectMake(25*PERCEN, (100*PERCEN - 35*PERCEN*headImgView.image.size.height/headImgView.image.size.width)/2., 35*PERCEN, 35*PERCEN*headImgView.image.size.height/headImgView.image.size.width);
                    titleLb.text = @"国学";
                    levelLb.text = [NSString stringWithFormat:@"%d首",(int)([[_mutArr objectAtIndex:2] count])];
                }
                    break;
                case 2:
                {
                    headImgView.image = [UIImage imageNamed:@"gushi_kj"];
                    headImgView.frame = CGRectMake(25*PERCEN, (100*PERCEN - 35*PERCEN*headImgView.image.size.height/headImgView.image.size.width)/2., 35*PERCEN, 35*PERCEN*headImgView.image.size.height/headImgView.image.size.width);
                    titleLb.text = @"故事";
                    levelLb.text = [NSString stringWithFormat:@"%d首",(int)([[_mutArr objectAtIndex:3] count])];
                }
                    break;
                case 3:
                {
                    headImgView.image = [UIImage imageNamed:@"yingwen_kj"];
                    headImgView.frame = CGRectMake(25*PERCEN, (100*PERCEN - 35*PERCEN*headImgView.image.size.height/headImgView.image.size.width)/2., 35*PERCEN, 35*PERCEN*headImgView.image.size.height/headImgView.image.size.width);
                    titleLb.text = @"英文";
                    levelLb.text = [NSString stringWithFormat:@"%d首",(int)([[_mutArr objectAtIndex:4] count])];
                }
                    break;
                case 4:
                {
                    headImgView.image = [UIImage imageNamed:@"qinzixiu_kj"];
                    headImgView.frame = CGRectMake(25*PERCEN, (100*PERCEN - 35*PERCEN*headImgView.image.size.height/headImgView.image.size.width)/2., 35*PERCEN, 35*PERCEN*headImgView.image.size.height/headImgView.image.size.width);
                    titleLb.text = @"亲子秀";
                    levelLb.text = [NSString stringWithFormat:@"%d首",(int)([[_mutArr objectAtIndex:5] count])];
                    
                }
                    break;
                    
                default:
                    break;
            }
        }
            break;
            
        default:
            break;
    }
    if (ISIPAD) {
        headImgView.left = 460*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.);
        titleLb.left = headImgView.right+20*PERCEN;
        UIImage *rightImg = [UIImage imageNamed:@"arrow"];
        rightView.frame = CGRectMake(SCREEN_WIDTH-460*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.)-rightImg.size.width, (100*PERCEN-rightImg.size.height)/2., rightImg.size.width, rightImg.size.height);
        levelLb.frame = CGRectMake(rightView.right-30*PERCEN-120, titleLb.top, 120, 30);
        lineView.frame = CGRectMake(440*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.), 100*PERCEN-1, SCREEN_WIDTH-880*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.), 1);
    } else {
        headImgView.frame = CGRectMake(25*PERCEN, 32.5*PERCEN, 35*PERCEN, 35*PERCEN);
        titleLb.left = headImgView.right+20*PERCEN;
        UIImage *rightImg = [UIImage imageNamed:@"arrow"];
        rightView.frame = CGRectMake(SCREEN_WIDTH-25*PERCEN-rightImg.size.width, (100*PERCEN-rightImg.size.height)/2., rightImg.size.width, rightImg.size.height);
        levelLb.frame = CGRectMake(rightView.right-30*PERCEN-120, titleLb.top, 120, 30);
        lineView.frame = CGRectMake(0, 100*PERCEN-1, SCREEN_WIDTH, 1);
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100*PERCEN;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
    bgView.backgroundColor = [UIColor clearColor];
    
    UILabel *_nickNameLb = [[UILabel alloc]initWithFrame:CGRectMake((ISIPAD?460*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):25*PERCEN), 5, 200, 20)];
    _nickNameLb.backgroundColor = [UIColor clearColor];
    _nickNameLb.textAlignment = NSTextAlignmentLeft;
    _nickNameLb.textColor = UIColorFromHex(0x999999);
    _nickNameLb.font = [UIFont systemFontOfSize:13.f];
    if (section == 0) {
        _nickNameLb.text = @"全部";
    }
    else if (section == 1) {
        _nickNameLb.text = @"我的分类";
    }
    [bgView addSubview:_nickNameLb];
    
    return bgView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    MBSpaceAddMusicDetailViewController *addMusic = [[MBSpaceAddMusicDetailViewController alloc]init];
    NSString *titleString = @"全部下载";
    NSString *fileIDStr = @"";
    NSArray *filesArr = [UserManager standerDefult].spaceFileArr;
    if (filesArr.count == 0) return;
    
     NSArray *fileArr = filesArr[0];
    if (indexPath.section == 0) {
        titleString = @"全部下载";
        fileIDStr = @"0";
    }
    else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            titleString = @"儿歌";
            
        }
        else if (indexPath.row == 1) {
            titleString = @"国学";
        }
        else if (indexPath.row == 2) {
            titleString = @"故事";
        }
        else if (indexPath.row == 3) {
            titleString = @"英文";
        }
        else if (indexPath.row == 4) {
            titleString = @"亲子秀";
        }
        for (NSDictionary *dd in fileArr) {
            NSString *title = [dd objectForKey:@"CateName"];
            if ([title isEqualToString:titleString]) {
                fileIDStr = [NSString stringWithFormat:@"%@",[dd objectForKey:@"CateID"]];
            }
        }
    }
    addMusic.titleString = titleString;
    if (self.type == MBSpaceAddMusicType_Collection) {
        addMusic.showName = @"收藏";
    }
    else if (self.type == MBSpaceAddMusicType_SetAlbum) {
        addMusic.showName = [NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"CateName"]];
    }
    
    if (indexPath.section == 0) {
        addMusic.dataArr = [_mutArr objectAtIndex:0];
    } else {
        NSArray *arr = [_mutArr objectAtIndex:indexPath.row+1];
        addMusic.dataArr = arr;
    }
    addMusic.cateID = [NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"CateID"]];
    addMusic.cateFileID = fileIDStr;
    addMusic.type = _type;
    
    addMusic.dataDic = _dataDic;
    [self.navigationController pushViewController:addMusic animated:YES];
}

@end
